<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./lib/vue2.4.4.js"></script>
</head>

<body>
    <div id="app">
        <input type="text" v-model="inputVal" @keyup.enter="funcInputEnter">
        <p v-for="item in list" :key="item.id">
            <input type="checkbox">
            {{item.name}}
        </p>
    </div>

    <script>
        var vm = new Vue({
            el: '#app',
            data: {
                inputVal: '',
                list: [
                    {id: 1, name: '嬴政'},
                    {id: 2, name: '李斯'},
                    {id: 3, name: '赵高'},
                    {id: 4, name: '韩非'},
                    {id: 5, name: '荀子'}
                ]
            },
            methods: {
                funcInputEnter() {
                    // this.replace(/(^\s*)|(\s*$)/g, "")
                    if (this.inputVal.replace(/(^\s*)|(\s*$)/g, "").length === 0) return;
                    var maxId = -1;
                    this.list.forEach((item)=>{
                        maxId = Math.max(item.id, maxId);
                    });
                    this.list.unshift({
                        id: maxId+1,
                        name: this.inputVal
                    });
                    this.inputVal = '';
                }
            }
        });
    </script>
</body>

</html>